package com.itheima.service.store.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.store.QuestionItemDao;
import com.itheima.domain.store.QuestionItem;
import com.itheima.factory.MapperFactory;
import com.itheima.service.store.QuestionItemService;
import com.itheima.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;

public class QuestionItemServiceImpl implements QuestionItemService {


    @Override
    public QuestionItem findById(String id) {
        SqlSession sqlSession = null;
        QuestionItem questionItem = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            QuestionItemDao mapper = sqlSession.getMapper(QuestionItemDao.class);
            questionItem = mapper.findById(id);
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            TransactionUtil.close(sqlSession);
        }

        return questionItem;
    }

    @Override
    public void delete(QuestionItem questionItem) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            QuestionItemDao mapper = sqlSession.getMapper(QuestionItemDao.class);
            mapper.delete(questionItem.getId());
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public void save(QuestionItem questionItem) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            QuestionItemDao mapper = sqlSession.getMapper(QuestionItemDao.class);
            questionItem.setId(UUID.randomUUID().toString());

            mapper.save(questionItem);
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            TransactionUtil.close(sqlSession);
        }


    }

    @Override
    public void update(QuestionItem questionItem) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            QuestionItemDao mapper = sqlSession.getMapper(QuestionItemDao.class);
            mapper.update(questionItem);
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            TransactionUtil.close(sqlSession);
        }


    }

    @Override
    public PageInfo findAll(String questionId,Integer pageNum, Integer pageSize) {
        PageInfo<QuestionItem> questionItemPageInfo = null;
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            QuestionItemDao mapper = sqlSession.getMapper(QuestionItemDao.class);
            PageHelper.startPage(pageNum, pageSize);
            List<QuestionItem> all = mapper.findAll(questionId);
            questionItemPageInfo = new PageInfo<>(all);
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            TransactionUtil.close(sqlSession);
        }
        return questionItemPageInfo;
    }
}
